//
//  AddressCell.m
//
//  Created by Olof Millberg on 2010-04-06.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "AddressCell.h"

@implementation AddressCell

@synthesize street;
@synthesize area;
@synthesize postalcode;
@synthesize city;
@synthesize mapButton;
@synthesize locationManager;
@synthesize address;

#pragma mark Initialization Methods

- (void) dealloc
{
	if (locationManager)
		[locationManager release];
	[super dealloc];
}

- (void) populateWithDataFromWorkOrder:(WorkOrder *)workOrder
{
	self.address = workOrder.address;
}

- (void) setAddress:(WOAddress *)anAddress
{
	[address release];
	address = [anAddress retain];
	street.text = address.street;
	area.text = address.area;
	postalcode.text = address.zipCode;
	city.text = address.city;
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
	CLLocation *currentLocation = [locationManager location];
	
	//Starting address - current position.
	CLLocationCoordinate2D currentCoordinate = [currentLocation coordinate];
	float startingLatitude = currentCoordinate.latitude;
	float startingLongitude = currentCoordinate.longitude;
	
	//Destination address - work order address.
	NSString *destinationStreet = address.street;
	NSString *destinationArea = address.area;
	NSString *destinationCity = address.city;
	
	//Create the url.
	NSString* url = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=%f,%f&daddr=%@,%@,%@",
					 startingLatitude, 
					 startingLongitude,
					 [destinationStreet stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
					 [destinationArea stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
					 [destinationCity stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
	
	//(Magical) launch of Google Maps.
	[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];	
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
	if ([error code] == kCLErrorDenied) {
		[locationManager stopUpdatingLocation];
		[locationManager release];
		locationManager = nil;
	}
}

#pragma mark Google Maps Methods

/* Google Maps icon is tapped */
- (IBAction) mapWanted:(id)sender
{
	UIAlertView *alertView = [[UIAlertView alloc]
							  initWithTitle:@"Kartval" 
							  message:@"Google Maps kommer att startas och ta dig ut ur Trawoh."
							  delegate:self 
							  cancelButtonTitle:@"Tillbaka" 
							  otherButtonTitles:@"Vägbeskrivning", @"Position",nil];
	[alertView show];							 
}

/* Google Maps is launched */
- (void) showLocation
{	
	//Location addreess - work order address.
	NSString *destinationStreet = address.street;
	NSString *destinationArea = address.area;
	NSString *destinationCity = address.city;
	
	//Create the url.
	NSString* url = [NSString stringWithFormat: @"http://maps.google.com/maps?q=%@,%@,%@&z=16",
					 [destinationStreet stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
					 [destinationArea stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
					 [destinationCity stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
	
	//(Magical) launch of Google Maps.
	[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];
}

/* Your direction is updated */
- (void) showDirections
{	
	//Set up CLLocationManager.
	if (!locationManager)
	{
		locationManager = [[CLLocationManager alloc] init];
		[locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
		[locationManager setDelegate:self];
	}
	[locationManager startUpdatingLocation];
}

#pragma mark -

#pragma mark UIAlertView Methods

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
	if(buttonIndex == 1)
	{	
		//Vägbeskrivning.
		[self showDirections];
	}
	else if(buttonIndex == 2)
	{ 
		//Position.
		[self showLocation];
	}
	[alertView release];	
}

#pragma mark -

@end